﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using NIEN_LUAN.Db;
using System.Windows.Forms;

namespace NIEN_LUAN
{
    public partial class FormLapPhieuNhap : Form
    {
        private DataContentDataContext db = new DataContentDataContext();
        public Db.NHACUNGCAP NhaCungCapAdd;
        private Db.PHIEUNHAP Phieunhapcanin;
        public FormThongKePhieuNhap frmChitietphieunhap;
        public FormTimKiemPhieuNhap frmXemCTPN;

        public FormLapPhieuNhap()
        {
            InitializeComponent();
            this.groupBox1.Enabled = false;

            this.btnThoatPN.Enabled = true;
            //this.btnLuuPN.Enabled = false;
            this.btnLuuPN.Enabled = false;
            this.btnHuyPN.Enabled = false;            
        }

        public FormLapPhieuNhap(FormThongKePhieuNhap frm,Db.PHIEUNHAP phieunhapcanin)
        {
            InitializeComponent();
            this.frmChitietphieunhap = frm;
            this.Phieunhapcanin = phieunhapcanin;
            this.groupBox1.Enabled = false ;
            this.btnVietPNmoi.Enabled = false;
            this.btnThoatPN.Enabled = true;
            //this.btnLuuPN.Enabled = false ;
            this.btnLuuPN.Enabled = false;
            this.btnHuyPN.Enabled = false;
            //this.dataGridViewDSCTPN.Enabled = false;
        }

        public FormLapPhieuNhap(FormTimKiemPhieuNhap frm, Db.PHIEUNHAP phieunhapcanin)
        {
            InitializeComponent();
            this.frmXemCTPN = frm;
            this.Phieunhapcanin = phieunhapcanin;
            this.groupBox1.Enabled = false;
            this.btnVietPNmoi.Enabled = false;
            this.btnThoatPN.Enabled = true;
            //this.btnLuuPN.Enabled = false ;
            this.btnLuuPN.Enabled = false;
            this.btnHuyPN.Enabled = false;
            //this.dataGridViewDSCTPN.Enabled = false;
        }

        private void btnHuyPN_Click(object sender, EventArgs e)
        {
            this.txtSoPN.ResetText();
            this.comboBoxNCC.ResetText();
            this.numericUDTiendatra.ResetText();
            this.numericUDTongtien.ResetText();
            this.txtSoPN.Focus();
            this.dataGridViewDSCTPN.Rows.Clear();
        }

        private void btnThoatPN_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnTimNCC_Click(object sender, EventArgs e)
        {
            var kq = new FormChiTietNhaCungCap(this).ShowDialog();
            if (kq == DialogResult.OK)
            {
                var ncc = (from m in db.NHACUNGCAPs
                           select m).ToList();
                bsNhaCungCap.DataSource = ncc;
                //bsNhaCungCap.ResetBindings(true);
                this.bsNhaCungCap.MoveLast();
                MessageBox.Show("Đã thêm thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            //var kq = new FormTimChiTietNCC(this).ShowDialog();
            //if (kq == DialogResult.OK)
            //{
            //    MessageBox.Show("Đã chọn nhà cung cấp " + this.comboBoxNCC.Text, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            ////}
        }

        //private void btnChonMH_Click(object sender, EventArgs e)
        //{
        //    var kq = new FormTimChiTietMatHang(this).ShowDialog();
        //    if (kq == DialogResult.OK)
        //    {
        //        //var loaihang = (from m in db.LOAIHANGs
        //        //                select m).ToList();

        //        //this.

        //    }
        //    //Form frmTimChiTietMH = new FormTimChiTietMatHang();
        //    //frmTimChiTietMH.ShowDialog();
        //}

        private void FormLapPhieuNhap_Load(object sender, EventArgs e)
        {
            this.dataGridViewDSCTPN.AutoGenerateColumns = false;
            var danhsachnhacungcap = (from m in db.NHACUNGCAPs
                                      select m).ToList();
            this.bsNhaCungCap.DataSource = danhsachnhacungcap;
            this.comboBoxNCC.DataSource = bsNhaCungCap;
            this.comboBoxNCC.DisplayMember = "MaNhaCungCap";

            if ((frmChitietphieunhap != null && Phieunhapcanin != null) || (frmXemCTPN != null && Phieunhapcanin != null))
            {
                this.txtSoPN.Text = Phieunhapcanin.SoPhieuNhap;
               // this.txtSoCT.Enabled = false;
                this.txtMaNV.Text = Phieunhapcanin.MaNhanVien;
                this.comboBoxNCC.Text = Phieunhapcanin.NHACUNGCAP.MaNhaCungCap;
                this.numericUDTongtien.Value = Phieunhapcanin.TongTien ?? 0;
                this.numericUDTiendatra.Value = Phieunhapcanin.TienDaTra ?? 0;
                this.dateTimePickerHanttoan.Value = Phieunhapcanin.HanThanhToan ?? DateTime.Now;
                this.dateTimePicker1.Value = Phieunhapcanin.NgayLap ?? DateTime.Now;

                var chitietphieunhap = (from m in db.CHITIETPHIEUNHAPs
                                      where m.SoPhieuNhap == Phieunhapcanin.SoPhieuNhap
                                      select new Db.ChitietNhapXuat
                                      {
                                          //DonGia = m.DonGiaXuat ?? 0,
                                          GiaNhap=m.DonGiaNhap??0,
                                          MaHang = m.MaHang,
                                          SoLuong = m.SoLuongNhap ?? 0,
                                          TenHang = m.MATHANG.TenHang

                                      }).ToList();

                this.cHITIETPHIEUNHAPBindingSource.DataSource = chitietphieunhap;
            }
        }

        private void btnVietPNmoi_Click(object sender, EventArgs e)
        {
            this.txtSoPN.ResetText();
            this.comboBoxNCC.ResetText();
            this.numericUDTiendatra.ResetText();
            this.numericUDTongtien.ResetText();
            this.txtSoPN.Focus();
            dataGridViewDSCTPN.Rows.Clear();

            this.btnVietPNmoi.Enabled=false;
            this.btnLuuPN.Enabled = true;
            this.btnHuyPN.Enabled = true;
            this.btnInPN.Enabled=false;
            this.btnThoatPN.Enabled = true;
            this.btnChonMH.Enabled = true;            
            this.groupBox1.Enabled = true;
            this.txtMaNV.Text = global.MaNhanVienDangNhap;
        }

        private void btnLuuPN_Click(object sender, EventArgs e)
        {
            var chitietnhapxuat = this.cHITIETPHIEUNHAPBindingSource.DataSource as List<ChitietNhapXuat>;
            if (chitietnhapxuat == null)
            {
                MessageBox.Show("Chưa chọn hàng!");
                return;
            }
            if (chitietnhapxuat.Count == 0)
            {
                MessageBox.Show("Chưa chọn hàng");
                return;
            }
            var nhacungcap = this.bsNhaCungCap.Current as NHACUNGCAP;

            if (nhacungcap==null)
            {
                MessageBox.Show("Chưa chọn nhà cung cấp");
                return;
            }           
            if (string.IsNullOrEmpty(this.txtSoPN.Text))
            {
                MessageBox.Show("Chưa nhập số phiêu nhập");
                return;
            }

            var sopn = (from m in db.PHIEUNHAPs
                        where m.SoPhieuNhap == this.txtSoPN.Text.Trim()
                        select m).ToList();
            if (sopn.Count > 0)
            {
                MessageBox.Show("Số phiếu nhập này đã tồn tại!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (this.numericUDTiendatra.Value==0)
            {
                MessageBox.Show("Chưa nhập tiền đã trả");
                return;
            }
            if (this.numericUDTiendatra.Value > this.numericUDTongtien.Value)
            {
                MessageBox.Show("Tiền đã trả lớn hớn tổng tiền");
                return;
            }

            var phieunhap = new PHIEUNHAP
            {
                NgayLap = DateTime.Now,
                SoPhieuNhap = this.txtSoPN.Text,
                TienDaTra = this.numericUDTiendatra.Value,
                TongTien = this.numericUDTongtien.Value,              
                MaNhaCungCap = nhacungcap.MaNhaCungCap,
                MaNhanVien = global.MaNhanVienDangNhap
            };
            List<CHITIETPHIEUNHAP> chitietphieunhap = new List<CHITIETPHIEUNHAP>();
            foreach (var item in chitietnhapxuat)
            {
                chitietphieunhap.Add(new CHITIETPHIEUNHAP
                {
                    DonGiaNhap = item.DonGia,
                    MaHang = item.MaHang,
                    SoPhieuNhap = phieunhap.SoPhieuNhap,
                    SoLuongNhap = item.SoLuong
                    

                });
            }

            db.PHIEUNHAPs.InsertOnSubmit(phieunhap);
            db.CHITIETPHIEUNHAPs.InsertAllOnSubmit(chitietphieunhap);
            foreach (var item in chitietphieunhap)
            {
               var mathang= db.MATHANGs.Single(m => m.MaHang == item.MaHang);
               mathang.SoLuongHienCo = mathang.SoLuongHienCo + item.SoLuongNhap;
            }
            db.SubmitChanges();

            MessageBox.Show("Tạo phiếu nhập thành công!");
            //this.btnLuuPN.Enabled = false;
            //this.btnInPN.Enabled = true;
            //this.btnVietPNmoi.Enabled = true;
            //this.btnThoatPN.Enabled = true;
            //this.btnHuyPN.Enabled = false;
            //this.groupBox1.Enabled = false;
            //this.Close();
            this.btnInPN.Enabled = true;
            this.btnLuuPN.Enabled = false;
            this._phieunhap = phieunhap;
        }
        private PHIEUNHAP _phieunhap;
        private void btnChonMH_Click(object sender, EventArgs e)
        {
            Form frmThemCTMH = new FormTimChiTietMatHang(this);
            var kq = frmThemCTMH.ShowDialog();

            if (kq == DialogResult.OK)
            {
                //var loaihang = (from m in db.LOAIHANGs
                //                select m).ToList();
                //bsLoaiHang.DataSource = loaihang;
                //bsLoaiHang.ResetBindings(true);

                //MessageBox.Show("Thêm thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                //this.bsLoaiHang.MoveLast();
            }
        }

        private void btnChon_Click(object sender, EventArgs e)
        {
            Form frmChonNCC = new FormTimKiemNhaCungCap(this);
            frmChonNCC.ShowDialog();
        }

        private void btnInPN_Click(object sender, EventArgs e)
        {
            if (this.frmChitietphieunhap != null && this.Phieunhapcanin != null)
            {
                var frm=new FormReportChiTietPhieuNhap(this,Phieunhapcanin.SoPhieuNhap);
                frm.ShowDialog();
            }
            else
            {
                //sửa lại hàm tạo
                Form frmInCTPN = new FormReportPhieuNhap(this, _phieunhap);
                frmInCTPN.ShowDialog();
            }
        }     
    }
}
